<?php
// +----------------------------------------------------------------------
// | FastXCMS [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.fastxcms.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use cmf\controller\HomeBaseController;
use app\portal\model\PortalCategoryModel;
use app\portal\service\PostService;
use app\portal\model\PortalPostModel;
use think\Db;

// 模块参数详情程序安装
global $configinfo;
$configinfo['detailmodule'] = 1;

class ArticleController extends HomeBaseController
{
    public function index()
    {
		global $configblock,$configinfo,$configindex,$configbase,$configlang;

        $portalCategoryModel = new PortalCategoryModel();
        $postService         = new PostService();

        $articleId  = $this->request->param('id', 0, 'intval');
        $categoryId = $this->request->param('cid', 0, 'intval');
		
		// 【模块参数程序安装】文章详情条件配置模块
		$configmodule="detail";include_once(MODULEPARA_PATH.'/configmodule/configmodule/configmodulewhere.php');
		
		// 自定义模型获取当前模块栏目信息
		if($configinfo['modelok'] == 1)
		{
			$articleId = $configinfo['modeldataid'];
			$modelArticleId  = $this->request->param('id', 0, 'intval');
			$this->assign('modelArticleId', $modelArticleId);
			$article    = $configinfo['modelConfig'];
		}else{
			$article    = $postService->publishedArticle($articleId, $categoryId);	
		}

        if (empty($article)) {
            abort(404, $configlang['articlenotisset']);
        }

        $prevArticle = $postService->publishedPrevArticle($articleId, $categoryId);
        $nextArticle = $postService->publishedNextArticle($articleId, $categoryId);

        $tplName = 'article';
            
		$category = $portalCategoryModel->where('id', $categoryId)->where('status', 1)->find();

		if (empty($category)) {
			abort(404, $configlang['articlenotcate']);
		}

		$this->assign('category', $category);

		$tplName = empty($category["one_tpl"]) ? $tplName : $category["one_tpl"];

        Db::name('portal_post')->where(['id' => $articleId])->setInc('post_hits');


        hook('portal_before_assign_article', $article);

        $this->assign('article', $article);
        $this->assign('prev_article', $prevArticle);
        $this->assign('next_article', $nextArticle);

        $tplName = empty($article['more']['template']) ? $tplName : $article['more']['template'];
		
		// 页页标题，关键词，描述
		$headmeta = seoheadmeta($article['post_title'],$article['post_keywords'],$article['post_excerpt'],$configbase['config']['name'],$article['post_content']);
		$this->assign('headmeta', $headmeta);
		
		// 【模块参数程序安装】文章详情配置模块
		$configmodule="detail";include_once(MODULEPARA_PATH.'/configmodule/configmodule/configmodule.php');

		if(@$parametersmarty && $tplName == "article")
		{
			return $this->fetch('/' . $parametersmarty);
		
		}else{	
			return $this->fetch("/$tplName");
		}
    }

    // 文章点赞
    public function doLike()
    {
		global $configlang;
		
        $this->checkUserLogin();
        $articleId = $this->request->param('id', 0, 'intval');


        $canLike = cmf_check_user_action("posts$articleId", 1);

        if ($canLike) {
            Db::name('portal_post')->where(['id' => $articleId])->setInc('post_like');

            $this->success($configlang['Good_job']);
        } else {
            $this->error($configlang['You_already_liked_it']);
        }
    }

}
